/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.network.topo.interaction;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import twaver.Element;
import twaver.Link;
import twaver.TWaverConst;
import twaver.network.CanvasMarker;
import twaver.network.TNetwork;
import twaver.network.inputhandler.InputAdapter;
import twaver.network.ui.LinkUI;

public class MouseHandler extends InputAdapter implements CanvasMarker {

	private TNetwork network;
	private Ellipse2D shape = null;
	private Color color = new Color(0, 255, 0, 128);
	
	public MouseHandler(TNetwork network){
		this.network = network;
		this.network.getCanvas().addMouseListener(this);
		this.network.getCanvas().addMouseMotionListener(this);
		this.network.addCanvasMarker(this);
	}
	
    public void mouseMoved(MouseEvent e) {
    	this.network.getCanvas().setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
    }
	
    public void mousePressed(MouseEvent e) {
    	Element element = network.getElementPhysicalAt(e.getPoint());
    	Shape oldShape = this.shape;
    	if(element != null){
    		shape = null;
    	}else{
    		int w = 15;
    		Point point = network.getLogicalPoint(e.getPoint());
    		shape = new Ellipse2D.Float(point.x-w, point.y-w, 2*w, 2*w);
    	}
    	if(oldShape != null){
    		network.getRenderer().repaintLogicalBounds(oldShape.getBounds());
    	}
    	if(shape != null){
    		network.getRenderer().repaintLogicalBounds(shape.getBounds());
    	}
    }

    public void mouseDragged(MouseEvent e) {
    	if(shape != null){
       		network.getRenderer().repaintLogicalBounds(shape.getBounds());
    		shape = null;
    	}
    }
    
    public void mouseReleased(MouseEvent e) {
    	if(shape != null){
    		List list = new ArrayList();
    		Rectangle bounds = shape.getBounds();
    		Iterator it = network.getDataBox().iterator();
    		while(it.hasNext()){
    			Element element = (Element)it.next();
    			if(element instanceof Link){
    				LinkUI ui = (LinkUI)network.getElementUI(element);
    				GeneralPath path = ui.getPath();
    				if(path.intersects(bounds)){
    					list.add(element);
    				}
    			}
    		}
    		if(list.size() > 0){
    			network.getDataBox().getSelectionModel().setSelection(list);
    		}
    		network.getRenderer().repaintLogicalBounds(shape.getBounds());
    		shape = null;
    	}
    }
	
	public void mark(Graphics2D g) {
		if(shape != null){
			g.setColor(color);
			g.fill(shape);
			g.setColor(Color.WHITE);
			g.setStroke(TWaverConst.DASHED_STROKE);
			g.draw(shape.getBounds());
		}
	}

}